home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / TEXAS XFCNs ƒ / mergeIndicesXFCN ƒ / open_all_files.z1.c < prev   
Encoding:
C/C++ Source or Header  |  1987-12-30  |  6.5 KB  |  236 lines  |  [TEXT/KAHL]

  1. /* routine to open all files that will be needed for the work...
  2.  */
  3.  
  4. #include "mergeIndices.z.h"
  5.  
  6. extern Str255 fn[3][3];
  7. extern int vRef[3][3], refNum[3][3], x0, y0;
  8. extern long first_text_file_size;
  9.  
  10.  
  11. int open_all_files ()
  12.   {
  13.     give_msg ("\pChoose first dataspace to be merged (second will be appended to it)...");
  14.     if (! getTextFile (FIRSTIN))
  15.         return (0);
  16.     give_msg ("\pChoose second dataspace to be merged (follows previous choice)...");
  17.     if (! getTextFile (SECONDIN))
  18.         return (0);
  19.     give_msg ("\pName the merged file -- in same folder as first dataspace, please!");
  20.     if (! putTextFile ())
  21.         return (0);
  22.     
  23.     return (1);
  24.   }
  25.   
  26.  
  27. /* routine to get a text file name, vRef, and refNum ... first for a brwsr
  28.  * database document file, then for the affiliated key and ptr files.
  29.  * If a .k or .p file isn't found, ask the user for help.... use SFGetFile
  30.  * approach.... flag success with 1, return 0 if failure or user cancel...
  31.  */
  32.  
  33. int getTextFile (filenum)
  34.   int filenum;
  35.   {
  36.     if (! getFileZ (&fn[filenum][TEXTFILE], &vRef[filenum][TEXTFILE]))
  37.         return (0);
  38.     if (FSOpen (&fn[filenum][TEXTFILE], vRef[filenum][TEXTFILE],
  39.                     &refNum[filenum][TEXTFILE]) != noErr)
  40.       {
  41.           give_msg ("\pFatal error opening dataspace text file!  Click mouse to exit...");
  42.           beepWait ();
  43.           return (0);
  44.       }
  45.     
  46.     if (filenum == FIRSTIN &&
  47.             GetEOF (refNum[filenum][TEXTFILE], &first_text_file_size)
  48.                     != noErr)
  49.       {
  50.           give_msg ("\pFatal error getting text file length!  Click mouse to exit...");
  51.           beepWait ();
  52.           return (0);
  53.       }
  54.     
  55.     pStrCopy ((char *)fn[filenum][TEXTFILE], (char *)fn[filenum][KEYFILE]);
  56.     *(fn[filenum][KEYFILE] + *fn[filenum][KEYFILE] + 1) = '.';
  57.     *(fn[filenum][KEYFILE] + *fn[filenum][KEYFILE] + 2) = 'k';
  58.     *fn[filenum][KEYFILE] += 2;
  59.     vRef[filenum][KEYFILE] = vRef[filenum][TEXTFILE];
  60.     
  61.     if (FSOpen (&fn[filenum][KEYFILE], vRef[filenum][KEYFILE],
  62.                     &refNum[filenum][KEYFILE]) != noErr)
  63.       {
  64.         give_msg ("\pCan't find dataspace key file -- please locate it...");
  65.         if (! getFileZ (&fn[filenum][KEYFILE], &vRef[filenum][KEYFILE]))
  66.             return (0);
  67.         if (FSOpen (&fn[filenum][KEYFILE], vRef[filenum][KEYFILE],
  68.                     &refNum[filenum][KEYFILE]) != noErr)
  69.             {
  70.               give_msg ("\pFatal error opening dataspace key file!  Click mouse to exit...");
  71.               beepWait ();
  72.               return (0);
  73.             }
  74.       }
  75.     
  76.     pStrCopy ((char *)fn[filenum][TEXTFILE], (char *)fn[filenum][PTRFILE]);
  77.     *(fn[filenum][PTRFILE] + *fn[filenum][PTRFILE] + 1) = '.';
  78.     *(fn[filenum][PTRFILE] + *fn[filenum][PTRFILE] + 2) = 'p';
  79.     *fn[filenum][PTRFILE] += 2;
  80.     vRef[filenum][PTRFILE] = vRef[filenum][TEXTFILE];
  81.     
  82.     if (FSOpen (&fn[filenum][PTRFILE], vRef[filenum][PTRFILE],
  83.                     &refNum[filenum][PTRFILE]) != noErr)
  84.       {
  85.         give_msg ("\pCan't find dataspace ptr file -- please locate it...");
  86.         if (! getFileZ (&fn[filenum][PTRFILE], &vRef[filenum][PTRFILE]))
  87.             return (0);
  88.         if (FSOpen (&fn[filenum][PTRFILE], vRef[filenum][PTRFILE],
  89.                     &refNum[filenum][PTRFILE]) != noErr)
  90.             {
  91.               give_msg ("\pFatal error opening dataspace ptr file!  Click mouse to exit...");
  92.               beepWait ();
  93.               return (0);
  94.             }
  95.       }
  96.     
  97.     return (1);
  98.   }
  99.  
  100.  
  101. /* routine to get a name for the merged output text file, using SFPutFile
  102.  * approach ... then open the associated key and ptr files for it ...
  103.  * return 0 if cancel or failure, 1 if success...
  104.  *
  105.  * ... NOTE that the merged text file had better be in the same folder
  106.  * on the same disk as the first input file, so that our quick append
  107.  * method can work -- so warn the user, and refuse to accept a name
  108.  * anyplace else! ... also note that we don't actually create or open
  109.  * the merged text file, since we're going to do a fast append of the
  110.  * second text file to the end of the first one, in place, and then just
  111.  * rename the first file to the merged output name....
  112.  */
  113.  
  114. int putTextFile ()
  115.   {
  116.     SFTypeList myTypes;
  117.     Point SFPwhere;
  118.     SFReply reply;
  119.     Str255 defaultfn;
  120.  
  121.     SFPwhere.v = 106 + y0;
  122.     SFPwhere.h = 104 + x0;
  123.     
  124.     pStrCopy ("\pMerged Dataspace File", (char *)fn[MERGEDOUT][TEXTFILE]);
  125.         
  126.   tryagain:
  127.     SFPutFile(SFPwhere, "\p", fn[MERGEDOUT][TEXTFILE], 0L, &reply);
  128.     if (!reply.good)
  129.         return (0);
  130.     if (reply.vRefNum != vRef [FIRSTIN][TEXTFILE])
  131.       {
  132.         give_msg ("\pMerged dataspace MUST be in same folder as first one!  Try again...");
  133.         SysBeep (10);
  134.         goto tryagain;
  135.       }
  136.  
  137.     pStrCopy((char *)reply.fName, (char *)fn[MERGEDOUT][TEXTFILE]);
  138.     vRef[MERGEDOUT][TEXTFILE] = reply.vRefNum;
  139.     
  140.     pStrCopy ((char *)fn[MERGEDOUT][TEXTFILE],
  141.                     (char *)fn[MERGEDOUT][KEYFILE]);
  142.     *(fn[MERGEDOUT][KEYFILE] + *fn[MERGEDOUT][KEYFILE] + 1) = '.';
  143.     *(fn[MERGEDOUT][KEYFILE] + *fn[MERGEDOUT][KEYFILE] + 2) = 'k';
  144.     *fn[MERGEDOUT][KEYFILE] += 2;
  145.     vRef[MERGEDOUT][KEYFILE] = vRef[MERGEDOUT][TEXTFILE];
  146.     if (! CreateFile (&fn[MERGEDOUT][KEYFILE], &vRef[MERGEDOUT][KEYFILE],
  147.                     &refNum[MERGEDOUT][KEYFILE]))
  148.       {
  149.               give_msg ("\pFatal error creating merged key file!  Click mouse to exit...");
  150.               beepWait ();
  151.               return (0);
  152.       }
  153.     
  154.     pStrCopy ((char *)fn[MERGEDOUT][TEXTFILE],
  155.                     (char *)fn[MERGEDOUT][PTRFILE]);
  156.     *(fn[MERGEDOUT][PTRFILE] + *fn[MERGEDOUT][PTRFILE] + 1) = '.';
  157.     *(fn[MERGEDOUT][PTRFILE] + *fn[MERGEDOUT][PTRFILE] + 2) = 'p';
  158.     *fn[MERGEDOUT][PTRFILE] += 2;
  159.     vRef[MERGEDOUT][PTRFILE] = vRef[MERGEDOUT][TEXTFILE];
  160.     if (! CreateFile (&fn[MERGEDOUT][PTRFILE], &vRef[MERGEDOUT][PTRFILE],
  161.                     &refNum[MERGEDOUT][PTRFILE]))
  162.       {
  163.               give_msg ("\pFatal error creating merged ptr file!  Click mouse to exit...");
  164.               beepWait ();
  165.               return (0);
  166.       }
  167.     
  168.     return (1);
  169.   }
  170.  
  171.  
  172.  
  173. /* create a file, based on LSC demo prog... give it my ^z filetype...
  174.  */ 
  175.  
  176. int CreateFile (fn, vRef, theRef)
  177.   Str255    *fn;
  178.   int        *vRef;
  179.   int        *theRef;
  180.   {
  181.     int io;
  182.     
  183.     io = Create (fn, *vRef, '????', 'CTLZ');
  184.     if ((io == noErr) || (io == dupFNErr))
  185.         io = FSOpen (fn, *vRef, theRef);
  186.     return ((io == noErr) || (io == dupFNErr));
  187.   }
  188.  
  189.  
  190.  
  191. /* following variables and routine do the standard files dialog
  192.  * to get the name of the file to use ... cribbed from the MiniEdit
  193.  * example that comes with LSC....
  194.  */
  195.  
  196. int getFileZ (fnp, vRefp)
  197.   Str255 *fnp;
  198.   int *vRefp;
  199.   {
  200.     SFTypeList myTypes;
  201.     Point SFGwhere;
  202.     SFReply reply;
  203.  
  204.     SFGwhere.v = 90 + y0;
  205.     SFGwhere.h = 82 + x0;
  206.     myTypes[0] = 'TEXT';
  207.     SFGetFile (SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
  208.     if (reply.good)
  209.       {
  210.         pStrCopy ((char *)reply.fName, (char *)fnp);
  211.         *vRefp = reply.vRefNum;
  212.         return (1);
  213.       }
  214.     else return (0);
  215.   }
  216.  
  217.  
  218. /* routine to copy a pascal string from one place to another.... used in
  219.  * above Standard Files routine....
  220.  */
  221.  
  222. void pStrCopy (p1, p2)
  223.   register char *p1, *p2;
  224.   {
  225.     register int len;
  226.     
  227.     len = *p2++ = *p1++;
  228.     while (--len >= 0)
  229.         *p2++ = *p1++;
  230.     return;
  231.   }
  232.  
  233.  
  234.  
  235.  
  236.